<?php

/**
 * QaActiveRecord class
 *
 * @author Julot
 * @since Friday, May 25, 2012. 10:36 PM
 */
class QaActiveRecord extends CActiveRecord {

	public function behaviors() {
		$behaviors = array(
			'TimeBehaviour'=>array(
				'class'=>'zii.behaviors.CTimestampBehavior',
				'createAttribute'=>'createdAt',
				'updateAttribute'=>'updatedAt',
				'setUpdateOnCreate'=>true,
				'timestampExpression'=>"date('Y-m-d H:i:s')",
			),
			'UserBehavior'=>array(
				'class'=>'zii.behaviors.CTimestampBehavior',
				'createAttribute'=>'createdBy',
				'updateAttribute'=>'updatedBy',
				'setUpdateOnCreate'=>true,
				'timestampExpression'=>"Yii::app()->user->id",
			),
		);
		return $behaviors;
	}

	public function __get($name) {
		$set = isset($this->getMetaData()->tableSchema->columns[$name]);
		$unix = false;
		$type = '';

		if (!$set) {
			if (strlen($name) > 4 && substr($name, 0, 4) == 'unix') {
				$unix = true;
				$name = strtolower($name[4]) . substr($name, 5);
			}
			$set = isset($this->getMetaData()->tableSchema->columns[$name]);
		}

		if ($set) {
			$type = $this->getMetaData()->tableSchema->columns[$name]->dbType;
		}

		$return = parent::__get($name);
		if ($return) {
			if ($unix && $type == "date") {
				return CDateTimeParser::parse($return, 'yyyy-MM-dd');
			} else if ($unix && $type == "datetime") {
				return CDateTimeParser::parse($return, 'yyyy-MM-dd hh:mm:ss');
			}
		} else {
			if ($return === '') return null;
		}

		return $return;
	}

}
